/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VocSymbTest2;

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;


/**
 *
 * @author StiggieBig
 */
public class VocabularySymbolSet<T extends VocabularySymbol> implements Iterable<T>{

    SortedSet<T> vocabularySymbolSet;

    public VocabularySymbolSet() {
        vocabularySymbolSet = new TreeSet<T>(new VocabComparator<T>());
    }

    public VocabularySymbolSet(T symb) {
    	vocabularySymbolSet = new TreeSet<T>(new VocabComparator<T>());
        vocabularySymbolSet.add(symb);
    }

    public VocabularySymbolSet(VocabularySymbolSet<T> copyFrom) {
        for (T tempVocabularySymbol : copyFrom.vocabularySymbolSet) {
            vocabularySymbolSet.add(tempVocabularySymbol);
        }
    }
    
    public boolean add(T toAdd){
    	return vocabularySymbolSet.add(toAdd);
    }
    
    public boolean isVocSymbElem(T vocSymb){
    	return vocabularySymbolSet.contains(vocSymb);
    }
    
    public boolean isCharElem(char charToCheck){
    	boolean isElem = false;
    	char tempCharVal;
    	Iterator<T> it = vocabularySymbolSet.iterator();
    	while(isElem == false && it.hasNext()){
            T tempVocabSymbol = it.next();
            tempCharVal = tempVocabSymbol.GetSymbolChar();
            if(charToCheck == tempCharVal){
            	isElem = true;
            }
        }
    	return isElem;
    }
    
    public int FindPosInSet(T vocSymb) {
        int pos = 0;
        if(vocabularySymbolSet.contains(vocSymb)){
        	pos = vocabularySymbolSet.headSet(vocSymb).size();
        }
        return pos;
    }
    
    public T GetVocSymbFromChar(char charSymbToGet){
    	boolean found = false;
    	T symbToFind = null;
    	Iterator<T> it = vocabularySymbolSet.iterator();
    	while(!found && it.hasNext()){
    		symbToFind = ((T)it.next());
            if(charSymbToGet == symbToFind.GetSymbolChar()){
            	found = true;
            }
        }
    	if(found){
    		return ((T) symbToFind);
    	}
    	return null;
    }
    
    public void Print() {
    	System.out.print("{");
        for (T tempVocabularySymbol : vocabularySymbolSet) {
            tempVocabularySymbol.Print();
            if(tempVocabularySymbol != vocabularySymbolSet.last()){
            	System.out.print(", ");
            }
        }
    	System.out.println("}");
    }
    
    public void PrintDebug(){
    	for (T tempVocabularySymbol : vocabularySymbolSet) {
            tempVocabularySymbol.PrintDebug();
        }
    }
    
    public void clear(){
    	vocabularySymbolSet.clear();
    }

	@Override
	public Iterator<T> iterator() {
		// TODO Auto-generated method stub
		return vocabularySymbolSet.iterator();
	}
}
